﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System.Management;

namespace AbstractClass.NPerf
{
    /// <summary>
    /// Represents the physical configuration of the machine.
    /// </summary>
    public class PerfMachine
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PerfMachine"/> class.
        /// </summary>
        public PerfMachine()
        {
            Ram = 0;
            Cpu = null;
            CpuHz = 0;
        }

        /// <summary>
        /// Gets or sets the physical memory size of the machine.
        /// </summary>
        /// <value>The physical memory size.</value>
        public long Ram { get; set; }

        /// <summary>
        /// Gets the physical memory size of the machine in MB.
        /// </summary>
        /// <value>The physical memory size of the machine in MB.</value>
        public double RamMb
        {
            get { return (double) Ram / (1024 * 1024); }
        }

        /// <summary>
        /// Gets or sets the cpu information of the machine.
        /// </summary>
        /// <value>The cpu information.</value>
        public string Cpu { get; set; }

        /// <summary>
        /// Gets or sets the cpu speed of the machine in Hz.
        /// </summary>
        /// <value>The cpu speed of the machine in Hz.</value>
        public long CpuHz { get; set; }

        /// <summary>
        /// Gets the cpu speed of the machine in MHz.
        /// </summary>
        /// <value>The cpu speed of the machine in MHz.</value>
        public double CpuMHz
        {
            get { return CpuHz / 1.0e6; }
        }

        /// <summary>
        /// Retrieves system information about the machine.
        /// </summary>
        /// <returns>A <see cref="PerfMachine"/> object describing
        /// the physical configuration of the machine.
        /// </returns>
        /// <remarks>
        /// This method queries the register for physical information on the 
        /// system.
        /// </remarks>
        public static PerfMachine GetCurrent()
        {
            lock (typeof (PerfMachine))
            {
                var machine = new PerfMachine();

                var query = new ManagementObjectSearcher("SELECT * From Win32_ComputerSystem");
                foreach (ManagementObject obj in query.Get())
                {
                    machine.Ram = long.Parse(obj["TotalPhysicalMemory"].ToString());
                    break;
                }

                query = new ManagementObjectSearcher("SELECT * From Win32_Processor");
                foreach (ManagementObject obj in query.Get())
                {
                    machine.Cpu = (string) obj["Name"];
                    machine.CpuHz = long.Parse(obj["CurrentClockSpeed"].ToString());
                    break;
                }

                return machine;
            }
        }
    }
}